package controller

import (
	"github.com/Unknwon/com"
	"github.com/gin-gonic/gin"
	"hytx_manager/models"
	"hytx_manager/pkg/setting"
	"strconv"
)

/**
	申请提现列表
 */
func PutForward(c *gin.Context) {
	status := c.Query("status")
	phone := c.Query("phone")
	beginTime := c.Query("begin_time")
	endTime := c.Query("end_time")

	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	limit, _ := strconv.Atoi(c.DefaultQuery("limit", strconv.Itoa(setting.AppSetting.PageSize)))

	res, paginate := models.PutFrowardList(page, limit, status, phone, beginTime, endTime)
	render(c, gin.H{
		"paginate": paginate,
		"data":     res,
	})
}

/**
	提现
 */
func MakeMoney(c *gin.Context) {
	idi, err := strconv.Atoi(c.PostForm("id"))
	if err != nil {
		fail(c, "参数错误")
		return
	}
	if err := models.MakeMoney(idi); err != nil {
		serverError(c)
		return
	}
	success(c)
}

func UserRechargesList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	limit, _ := strconv.Atoi(c.DefaultQuery("limit", strconv.Itoa(setting.AppSetting.PageSize)))

	data, paginate := models.UserRechargeList(page, limit, c)

	render(c, gin.H{
		"paginate": paginate,
		"data":     data,
	})
}

func UserProfitList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	limit, _ := strconv.Atoi(c.DefaultQuery("limit", strconv.Itoa(setting.AppSetting.PageSize)))

	data, paginate := models.UserProfitList(page, limit, c)

	render(c, gin.H{
		"paginate": paginate,
		"data":     data,
	})
}

func UserProfitDetail(c *gin.Context) {
	userId, isExist := c.GetQuery("user_id")
	if !isExist || userId == "" || userId == "0" {
		fail(c, "用户id不能为空")
		return
	}
	userIdi, _ := strconv.Atoi(userId)
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	limit, _ := strconv.Atoi(c.DefaultQuery("limit", strconv.Itoa(setting.AppSetting.PageSize)))

	data, paginate := models.UserProfitDetail(page, limit, userIdi, c)

	render(c, gin.H{
		"paginate": paginate,
		"data":     data,
	})
}

func PutForwardList(c *gin.Context) {
	userId, isExist := c.GetQuery("user_id")
	if !isExist || userId == "" || userId == "0" {
		fail(c, "用户id不能为空")
		return
	}
	userIdi, _ := strconv.Atoi(userId)
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	limit, _ := strconv.Atoi(c.DefaultQuery("limit", strconv.Itoa(setting.AppSetting.PageSize)))

	data, paginate := models.UserPutForwardList(page, limit, userIdi, c)

	render(c, gin.H{
		"paginate": paginate,
		"data":     data,
	})
}

func PutForwardListByDistributor(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	limit, _ := strconv.Atoi(c.DefaultQuery("limit", strconv.Itoa(setting.AppSetting.PageSize)))

	data, paginate := models.PutForwardListByDistributor(page, limit, c)

	render(c, gin.H{
		"paginate": paginate,
		"data":     data,
	})
}

func PutForwardByDistributor(c *gin.Context) {
	id := com.StrTo(c.PostForm("id")).MustInt()
	err := models.DB.Table("maibao_distributor_cash").Where("id=?",id).Update("status", 1).Error
	if err != nil {
		fail(c, err.Error())
		return
	}
	success(c)
}

func MaibaoLog(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	limit, _ := strconv.Atoi(c.DefaultQuery("limit", strconv.Itoa(setting.AppSetting.PageSize)))
	userType := c.DefaultQuery("user_type", "user")

	res, paginate := models.MaibaoRechargeLog(page, limit, c, userType)
	render(c, gin.H{
		"paginate": paginate,
		"data":     res,
	})
}

func MaibaoLogByUser(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	limit, _ := strconv.Atoi(c.DefaultQuery("limit", strconv.Itoa(setting.AppSetting.PageSize)))
	userType := c.DefaultQuery("user_type", "user")
	userId, _ := strconv.Atoi(c.Query("id"))

	res, paginate := models.MaibaoRechargeLogByUser(page, limit, c, userType, userId)
	render(c, gin.H{
		"paginate": paginate,
		"data":     res,
	})
}

func MaibaoRecharge(c *gin.Context) {
	userType := c.DefaultPostForm("user_type", "user")
	userId, _ := strconv.Atoi(c.PostForm("id"))
	amount, _ := com.StrTo(c.PostForm("amount")).Float64()

	res := models.MaibaoRecharge(userId, amount, userType)
	if res {
		success(c)
	}else {
		fail(c, "充值失败")
	}
}
func MaibaoSpendingLog(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	limit, _ := strconv.Atoi(c.DefaultQuery("limit", strconv.Itoa(setting.AppSetting.PageSize)))
	userType := c.DefaultQuery("user_type", "user")

	res, paginate := models.MaibaoSpendingLog(page, limit, c, userType)
	render(c, gin.H{
		"paginate": paginate,
		"data":     res,
	})
}

func MaibaoGain(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	limit, _ := strconv.Atoi(c.DefaultQuery("limit", strconv.Itoa(setting.AppSetting.PageSize)))
	userType := c.DefaultQuery("user_type", "user")
	userId, _ := strconv.Atoi(c.Query("id"))
	cate, _ := strconv.Atoi(c.DefaultQuery("type", "1"))

	data, paginate := models.MaibaoGain(page, limit, c, userType, userId, cate)

	render(c, gin.H{
		"paginate": paginate,
		"data":     data,
	})
}

func ManagerCenterRecharges(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	limit, _ := strconv.Atoi(c.DefaultQuery("limit", strconv.Itoa(setting.AppSetting.PageSize)))
	m := &models.ManageCenter{}
	data, paginate := m.Recharges(c, page, limit)

	render(c, gin.H{
		"paginate" : paginate,
		"data" : data,
	})
}

func ManagerCenterRechargesDetail(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	limit, _ := strconv.Atoi(c.DefaultQuery("limit", strconv.Itoa(setting.AppSetting.PageSize)))
	m := &models.ManageCenter{}
	t := c.Query("type")
	var data interface{}
	var paginate *models.Paginator
	if t == "user" {
		data, paginate = m.RechargesVipList(c, page, limit)
	}
	if t == "agent" {
		data, paginate = m.RechargesAgentList(c,page,limit)
	}
	if t == "e" {
		data, paginate = m.RechargesEList(c,page,limit)
	}
	render(c, gin.H{
		"data" : gin.H{
			"income": m.RechargesDetail(c, page, limit),
			"list" : data,
		},
		"paginate": paginate,
	})
}

func ManagerCenterRechargesCash(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	limit, _ := strconv.Atoi(c.DefaultQuery("limit", strconv.Itoa(setting.AppSetting.PageSize)))
	m := &models.ManageCenter{}
	data, paginate := m.Cash(c, page, limit)
	render(c, gin.H{
		"paginate" : paginate,
		"data" : data,
	})
}
func ManagerCenterRechargesCashStatus(c *gin.Context) {
	id, _ := strconv.Atoi(c.PostForm("id"))
	status, _ := strconv.Atoi(c.PostForm("status"))

	models.DB.Table("bonus_cash").Where("id=?", id).Where("type=1").Update("status", status)
	success(c)

}

func OperatorRecharges(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	limit, _ := strconv.Atoi(c.DefaultQuery("limit", strconv.Itoa(setting.AppSetting.PageSize)))
	o := &models.Operators{}
	data, paginate := o.Recharges(c, page, limit)

	render(c, gin.H{
		"paginate" : paginate,
		"data" : data,
	})
}

func OperatorRechargesDetail(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	limit, _ := strconv.Atoi(c.DefaultQuery("limit", strconv.Itoa(setting.AppSetting.PageSize)))
	m := &models.Operators{}
	t := c.Query("type")
	var data interface{}
	var paginate *models.Paginator
	if t == "user" {
		data, paginate = m.RechargesVipList(c, page, limit)
	}
	if t == "e" {
		data, paginate = m.RechargesEList(c,page,limit)
	}
	render(c, gin.H{
		"data" : gin.H{
			"income": m.RechargesDetail(c, page, limit),
			"list" : data,
		},
		"paginate": paginate,
	})
}
func OperatorRechargesCash(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	limit, _ := strconv.Atoi(c.DefaultQuery("limit", strconv.Itoa(setting.AppSetting.PageSize)))
	o := &models.Operators{}
	data, paginate := o.Cash(c, page, limit)
	render(c, gin.H{
		"paginate" : paginate,
		"data" : data,
	})
}
func OperatorRechargesCashStatus(c *gin.Context) {
	id, _ := strconv.Atoi(c.PostForm("id"))
	status, _ := strconv.Atoi(c.PostForm("status"))

	models.DB.Table("bonus_cash").Where("id=?", id).Where("type=2").Update("status", status)
	success(c)

}
func PartnerRecharges(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	limit, _ := strconv.Atoi(c.DefaultQuery("limit", strconv.Itoa(setting.AppSetting.PageSize)))
	p := &models.Partner{}
	data, paginate := p.Recharges(c, page, limit)

	render(c, gin.H{
		"paginate" : paginate,
		"data" : data,
	})
}
func PartnerRechargesDetail(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	limit, _ := strconv.Atoi(c.DefaultQuery("limit", strconv.Itoa(setting.AppSetting.PageSize)))
	p := &models.Partner{}
	t := c.Query("type")
	var data interface{}
	var paginate *models.Paginator
	if t == "user" {
		data, paginate = p.RechargesVipList(c, page, limit)
	}
	if t == "new" {
		data, paginate = p.RechargesNewList(c, page, limit)
	}
	if t == "e" {
		data, paginate = p.RechargesEList(c,page,limit)
	}
	render(c, gin.H{
		"data" : gin.H{
			"income": p.RechargesDetail(c, page, limit),
			"list" : data,
		},
		"paginate": paginate,
	})
}

func PartnerCenterRechargesCash(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	limit, _ := strconv.Atoi(c.DefaultQuery("limit", strconv.Itoa(setting.AppSetting.PageSize)))
	o := &models.Operators{}
	data, paginate := o.Cash(c, page, limit)
	render(c, gin.H{
		"paginate" : paginate,
		"data" : data,
	})
}
func PartnerCenterRechargesCashStatus(c *gin.Context) {
	id, _ := strconv.Atoi(c.PostForm("id"))
	status, _ := strconv.Atoi(c.PostForm("status"))

	models.DB.Table("bonus_cash").Where("id=?", id).Where("type=3").Update("status", status)
	success(c)

}